package com.example.helloworld;

import android.os.Bundle;
import android.widget.RadioGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;

import com.example.helloworld.base.BaseFragment;
import com.example.helloworld.fragment.GroupFragment;
import com.example.helloworld.fragment.LatestContactsFragment;
import com.example.helloworld.fragment.OrganizationFragment;
import com.example.helloworld.fragment.ThirdPartFragment;
import com.example.helloworld.listener.MyCheckChangeListener;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 主页面
 */
public class MainActivity extends FragmentActivity {

    private RadioGroup radioGroup;

    private Map<Integer,BaseFragment> baseFragmentMap;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //初始化主页
        initView();
        //初始化子页面
        initFragment();
        //设置radioGroup的监听
        setRadioGroupListener();

    }

    private void initView(){
        setContentView(R.layout.activity_main);
        //根据id找到对应的radioGroup
        radioGroup = findViewById(R.id.rg_main);
    }

    private void initFragment(){
        baseFragmentMap = new LinkedHashMap<>();
        baseFragmentMap.put(R.id.rb_latest_contacts_btn,new LatestContactsFragment());
        baseFragmentMap.put(R.id.rb_group_btn,new GroupFragment());
        baseFragmentMap.put(R.id.rb_organization_btn,new OrganizationFragment());
        baseFragmentMap.put(R.id.rb_third_part_btn,new ThirdPartFragment());
    }

    private void setRadioGroupListener(){
        radioGroup.setOnCheckedChangeListener(new MyCheckChangeListener(getSupportFragmentManager(),baseFragmentMap));
        //设置默认选中最近联系人
        radioGroup.check(R.id.rb_latest_contacts_btn);
    }
}
